package com.pluser.mq.engine;

import java.io.IOException;

import com.ibm.mq.MQException;
import com.ibm.mq.MQGetMessageOptions;
import com.ibm.mq.MQPutMessageOptions;
import com.ibm.mq.MQQueue;
import com.ibm.mq.MQQueueManager;
import com.pluser.mq.vo.QueueInfo;
import com.pluser.mq.vo.QueueManagerInfo;

/**
 * 
 * abstract handler class
 * @author pluser
 * @version 1.0
 * @date 2014-05-03
 * @modifiedHis
 *
 * @description
 * abstract handler class that implements handler interface,
 * and use default adapter
 */
public abstract class AbstractHandler implements IHandler {

	private MQQueueManager qMgr;
	private MQQueue queue;
	private QueueManagerInfo qmInfo;
	private QueueInfo qInfo;
	
	/**
	 * constructor
	 * @param qmInfo info of queue manager
	 * @param qInfo info of queue
	 * @throws MQException
	 */
	protected AbstractHandler(QueueManagerInfo qmInfo, QueueInfo qInfo) 
			throws MQException {
		
		this.qmInfo = qmInfo;
		this.qInfo = qInfo;
		
		this.qMgr = new MQQueueManager(this.qmInfo.getName(), 
				this.qmInfo.getProperties());
		this.queue = qMgr.accessQueue(this.qInfo.getName(),
				this.qInfo.getOpenOptions());
	}
	
	/**
	 * get MQQueue object
	 * @return 
	 */
	protected MQQueue getQueue() {
		return queue;
	}
	
	/**
	 * get QueueManagerInfo object
	 * @return
	 */
	public QueueManagerInfo getQueueManagerInfo() {
		return qmInfo;
	}
	
	/**
	 * get QueueInfo object
	 * @return
	 */
	public QueueInfo getQueueInfo() {
		return qInfo;
	}
	
	public void putMsg(byte[] msgBytes, MQPutMessageOptions pmo) 
			throws IOException, MQException {
	}
	
	public byte[] getMsg(MQGetMessageOptions gmo) 
			throws IOException, MQException {
		return null;
	}
	
	/**
	 * disconnect, close queue and queue manager 
	 * @throws MQException
	 */
	public void disconnect() throws MQException {
		
		if(queue != null && queue.isOpen()) {
			queue.close();
		}
		
		if(qMgr != null && qMgr.isConnected()) {
			qMgr.disconnect();
		}
	}
	
}
